% 脚本名称：Create_RGB_Image.m 
% 将光谱图像立方体处理成一张RGB图像
% 
% 需设置参数：① 拍摄图片文件夹路径
%             ② 棋盘格校正文件
%             ③ 光谱辐亮度标定文件
% 
% 保存：① 得到透过率校正系数a.mat

clc;clear;close all;
%% 需手动设置的参数
% dataPath = 'F:\前处理\结果\202203U型数据集\test\光谱图像\1190'; % 拍摄样本的文件夹路径
dataPath = 'F:\前处理\结果\202204矿石数据集\train\光谱图像\1001';
a_FileName = fullfile('F:\前处理\结果\202203贝壳数据集\test','a_326.mat');
white_reflectance_filename = 'F:\OneDrive\lyz资料\程序资料\软件相关\前处理\圆型光阑结果\反射率表.csv'; 
white_radiance_filename = 'F:\OneDrive\lyz资料\程序资料\实验数据\326数据\202203月底\17_iron\白板\白扳0331-140744.csv';
Save_Folder = '.';
targetSize = [380 480];
% white_balance = [0.9301,1.3964,6.4713]*3.5; % [R,G,B] 三通道的白平衡调节值
white_balance = [1.8027,2.4725,10.0606]*10; % [R,G,B] 三通道的白平衡调节值
bands = 420:10:690;
% 添加必要库
addpath(genpath('../utils'));

%%
tru_rad_cube = get_radiance(dataPath,bands,a_FileName);
win = centerCropWindow2d(size(tru_rad_cube),targetSize);
tru_rad_cube = tru_rad_cube(win.YLimits(1):win.YLimits(2),win.XLimits(1):win.XLimits(2),:);
% tru_ref_cube = radiance2reflectance(tru_rad_cube,bands,white_radiance_filename,white_reflectance_filename);

%% 真实图像rgb图
rgb_tru = cube2RGB(tru_rad_cube,bands,white_balance);
win = centerCropWindow2d(size(rgb_tru),targetSize);
rgb_tru = imcrop(rgb_tru,win);
imwrite(rgb_tru,fullfile(Save_Folder,'12.png'));
imshow(rgb_tru)
